package db21;

import db21.entities.Subjects;
import db21.listeners.ClickOnlyListener;
import java.awt.event.MouseEvent;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SubjectsView.java
 *
 * Created on 27.05.2013, 19:32:21
 */
/**
 *
 * @author Mamontov
 */
public class SubjectsView extends ApplicationPanel {

    /** Creates new form SubjectsView */
    public SubjectsView() {
        initComponents();
        JButton btns[] = {btnToCompetences, btnToThemes,  btnToMain };
        ApplicationMode modes[] = { 
            ApplicationMode.Competences,
            ApplicationMode.Themes,
            ApplicationMode.MainForm
        };
        for(int i = 0; i < btns.length; i++) {
            this.bindOnClickListenerTogglingToMode(btns[i], modes[i]);
        }
        bindSave(btnSave);
        
        DefaultListModel m = new DefaultListModel();
        lstAllSubjects.setModel(m);
        
        
        btnAdd.addMouseListener(new ClickOnlyListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                String individualName = SubjectsView.this.txtName.getText();
                if (individualName.isEmpty() == false) {                    
                    DefaultListModel k = (DefaultListModel)(SubjectsView.this.lstAllSubjects.getModel());
                    boolean result = m_model.resource(Subjects.class).makeIndividual(individualName);
                    if (result) {
                        k.addElement(individualName);
                    } else {
                         JOptionPane.showMessageDialog(SubjectsView.this, "Individual already exists");
                    }
                }
            }
        });
        
        btnDelete.addMouseListener(new ClickOnlyListener() {

            @Override
            public void mouseClicked(MouseEvent e) {
                JList c = SubjectsView.this.lstAllSubjects;
                DefaultListModel k = (DefaultListModel)(SubjectsView.this.lstAllSubjects.getModel());
                int si = c.getSelectedIndex();
                if (si != -1) {
                    String o = c.getSelectedValue().toString();
                    SubjectsView.this.m_model.resource(Subjects.class).removeIndividual(o);
                    k.remove(si);
                    if (si < k.size()) {
                        c.setSelectedIndex(si);
                    }
                }
            }
        });
        
        ListSelectionListener listSelectionListener = new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent listSelectionEvent) {
                DefaultListModel k = (DefaultListModel)(SubjectsView.this.lstAllSubjects.getModel());
                Integer index = SubjectsView.this.lstAllSubjects.getSelectedIndex();                
                boolean adjust = listSelectionEvent.getValueIsAdjusting();
                if (index != -1 && !adjust) {
                    String text = k.get(index).toString();
                    SubjectsView.this.txtName.setText(text);
                }
            }
        };
        lstAllSubjects.addListSelectionListener(listSelectionListener);
        
    }
    @Override
    public void fireModelUpdate() {
        this.m_model
            .resource(Subjects.class)
            .populateListWithEntities(lstAllSubjects);
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        lstAllSubjects = new javax.swing.JList();
        lblName = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        btnToCompetences = new javax.swing.JButton();
        btnToThemes = new javax.swing.JButton();
        btnToMain = new javax.swing.JButton();
        btnDelete = new javax.swing.JButton();
        btnAdd = new javax.swing.JButton();
        btnSave = new javax.swing.JButton();

        lstAllSubjects.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstAllSubjects);

        lblName.setText("Название");

        btnToCompetences.setText("К компетенциям");

        btnToThemes.setText("К темам");

        btnToMain.setText("К главному окну");

        btnDelete.setText("Удалить");

        btnAdd.setText("Добавить");

        btnSave.setText("Сохранить");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 354, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblName)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnToCompetences, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(btnToThemes, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                            .addComponent(btnSave, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(btnToMain, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnDelete, javax.swing.GroupLayout.DEFAULT_SIZE, 138, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 414, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnAdd)
                            .addComponent(btnSave)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(5, 5, 5)
                        .addComponent(btnDelete)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnToCompetences)
                            .addComponent(btnToThemes)
                            .addComponent(btnToMain))))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAdd;
    private javax.swing.JButton btnDelete;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnToCompetences;
    private javax.swing.JButton btnToMain;
    private javax.swing.JButton btnToThemes;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblName;
    private javax.swing.JList lstAllSubjects;
    private javax.swing.JTextField txtName;
    // End of variables declaration//GEN-END:variables
}
